package symboltable;

import java.util.HashMap;
import syntaxtree.*;

//representa uma classe nno codigo
//possui id, metodos e campos
public class ClassTable {

    private FieldsTable fieldsTable;
    private MethodsTable methodsTable;
    private String identifier;

    public ClassTable() {
        fieldsTable = new FieldsTable();
        methodsTable = new MethodsTable();
    }

    public MethodsTable addMethod(Identifier identifier, Type type) {
        if (methodsTable == null) {
            MethodsTable newMethodsTable = new MethodsTable();
            HashMap<Symbol, Type> hashmap = new HashMap<>();
            HashMap<Symbol, MethodTable> hashmapMT = new HashMap<>();

            if (!hashmap.containsKey(Symbol.symbol(identifier.s)))
                hashmap.put(Symbol.symbol(identifier.s), type);

            if (!hashmapMT.containsKey(Symbol.symbol(identifier.s))) {
                MethodTable methodTable = new MethodTable();
                hashmapMT.put(Symbol.symbol(identifier.s), methodTable);
            }

            newMethodsTable.setHashmapMethodsTable(hashmap);
            newMethodsTable.setHashmapMethodTable(hashmapMT);
            newMethodsTable.setIdAtual(identifier.s);
            setMethodsTable(newMethodsTable);
            return newMethodsTable;
        } else {
            methodsTable.getHashmapMethodsTable().put(Symbol.symbol(identifier.s), type);
            MethodTable methodTable = new MethodTable();
            methodsTable.getHashmapMethodTable().put(Symbol.symbol(identifier.s), methodTable);
            methodsTable.setIdAtual(identifier.s);
            return this.methodsTable;
        }
    }

    public void addFieldsTable(Identifier identifier, Type type) {
        if (this.fieldsTable == null) {
            FieldsTable newFieldsTable = new FieldsTable();
            newFieldsTable.addFields(identifier, type);
            setFieldsTable(newFieldsTable);
        } else
            fieldsTable.addFields(identifier, type);
    }

    public String getIdentifier() {
        return identifier;
    }

    public void setIdentifier(String identifier) {
        this.identifier = identifier;
    }

    public FieldsTable getFieldsTable() {
        return fieldsTable;
    }

    public void setFieldsTable(FieldsTable fieldsTable) {
        this.fieldsTable = fieldsTable;
    }

    public MethodsTable getMethodsTable() {
        return methodsTable;
    }

    public void setMethodsTable(MethodsTable methodsTable) {
        this.methodsTable = methodsTable;
    }
}
